package cate.game.role.note;

import cate.common.game.GameCollection;
import cate.game.role.Role;
import cate.game.role.RolePart;
import cate.game.role.note.msg.NoteResp;
import easy.java.dev.note.NoteField;
import easy.java.util.BasicTool;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.HashMap;
import java.util.Map;

@Document(GameCollection.NAME_ROLE_NOTE)
public class RoleNote extends RolePart {
    @NoteField(
            value = "笔记本",
            detail = "值只能是String或者Number")
    public Map<String, Object> book;

    public RoleNote(){}

    public RoleNote(Role role){
        super(role);
    }

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (book == null) {
            book = new HashMap<>();
        }
    }

    public void write(String key, Object value) {
        if (!BasicTool.isPrimitiveType(value)) {
            role.getGame().notice.message(role, "记录的值必须是简单类型");
            return;
        }
        book.put(key, value);
        role.sendNow(new NoteResp(role));
        if (key.startsWith("guide") && key.length() > 5) {
            String guideId = key.substring(5);
            try {
                role.getTrace().guideId = Integer.parseInt(guideId);
            } catch (Exception e) {
                logger.error("", e);
            }
        }
    }

    public Object read(String key) {
        return book.get(key);
    }
}
